package variabilitysmelldetection.measurement.models;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;

import variabilitysmelldetection.measurement.measures.Measure;


public abstract class Item {
	private static ASTParser parser;
	private String parent;
	private String name;
	private String source;
	@SuppressWarnings("rawtypes")
	private Map<Class, Measure> measures;
	private boolean isRefinement;
	private String path;
	
	@SuppressWarnings("rawtypes")
	public Item(String parent, String name) {
		this.parent = parent;
		this.name   = name;
		measures    = new LinkedHashMap<Class, Measure>();
		path        = "";
	}

	public String getParentName() {
		return parent;
	}
	
	public String getName() {
		return name;
	}
	
	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public Collection<Measure> getMeasures() {
		return measures.values();
	}

	public void addMeasure(Measure measure) {
		this.measures.put(measure.getClass(), measure);
	}
	
	public Measure getMeasure(Class<?> measure) {
		return measures.get(measure);
	}
	
	public boolean isRefinement() {
		return isRefinement;
	}

	public void setRefinement(boolean isRefinement) {
		this.isRefinement = isRefinement;
	}

	public String getPath(){
		return path;
	}
	
	public void setPath(String path){
		this.path = path;
	}
	
	public ASTNode getCompilationUnit(int kind) {	 		
		parser = ASTParser.newParser(AST.JLS4);
		parser.setKind(kind);		
		parser.setResolveBindings(true);
		parser.setSource(source.toCharArray());
		ASTNode node = parser.createAST(null);
	   /* IProblem[] problems = ((CompilationUnit)node).getProblems();
	    if (problems != null && problems.length > 0) {
	    	System.out.println();
	        System.out.println("Got problems compiling the source file: "+name);
	        for (IProblem problem : problems) {
	            System.out.println(problem);
	        }
	    }*/
	    return node; 
	}
		
	public abstract String getFieldsAndMeasuresNames();
	
	public abstract String toCSV();
}
